package com.yzf.enterprise.framework.web.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.io.Serializable;
import java.util.Date;

/**
 * Tk插件基础entity类
 *
 * @author AKing31
 * @version 1.0
 * @create 2019-11-15 12:51 PM
 */
@Data
public abstract class BaseTkEntity implements Serializable {

    private static final long serialVersionUID = -5665156117688243804L;

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "JDBC")
    @ApiModelProperty(hidden = true)
    protected Long id;

    /**
     * 删除状态
     * 0:未删除
     * 1:已删除
     */
    @Column(name = "deleted")
    @ApiModelProperty(hidden = true)
    protected Long deleted;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "create_time")
    @ApiModelProperty(hidden = true)
    protected Date createTime;

    /**
     * 创建人
     */
    @Column(name = "create_user")
    @ApiModelProperty(hidden = true)
    protected String createUser;

    /**
     * 最近更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "update_time")
    @ApiModelProperty(hidden = true)
    protected Date updateTime;

    /**
     * 更新人
     */
    @Column(name = "update_user")
    @ApiModelProperty(hidden = true)
    protected String updateUser;

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, false);
    }


    /**
     * 设置创建人和创建时间
     *
     * @param baseEntityObj baseEntity的子类对象
     * @param <T>           对象类型
     */
    @ApiModelProperty(hidden = true)
    public <T extends BaseTkEntity> void setUserAndTime(T baseEntityObj) {
        this.createTime = baseEntityObj.getCreateTime();
        this.createUser = baseEntityObj.getCreateUser();
        this.updateTime = baseEntityObj.getUpdateTime();
        this.updateUser = baseEntityObj.getUpdateUser();
    }
}
